package com.hustzp.com.xichuangzhu.me;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.XichuangzhuApplication;
import com.hustzp.com.xichuangzhu.login.LoginActivity;
import com.hustzp.com.xichuangzhu.utils.BrightnessUtil;
import com.hustzp.com.xichuangzhu.utils.DialogFactory;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.File;
import java.text.DecimalFormat;

/**
 * Created by chenxiaosong on 2016/5/29.
 */
public class SettingActivity extends MyBaseActivity {


    private LinearLayout loginSettingLayout;
    private LinearLayout logoutLayout;
    private SeekBar lightSkBar;

    private TextView fontType;
    private TextView fontStyle;

    private TextView cacheSizeTxt;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        initToolbar();
        initViews();
    }

    private void initToolbar() {
        TextView appTitle = (TextView) findViewById(R.id.tv_title_txt);
        appTitle.setText(getString(R.string.setting));
    }

    private void initViews() {
        loginSettingLayout = (LinearLayout) findViewById(R.id.settings_layout);
        logoutLayout = (LinearLayout) findViewById(R.id.logout_layout);

        fontType = (TextView) findViewById(R.id.tv_font_type);
        fontStyle = (TextView) findViewById(R.id.tv_font_style);
        initLoginCfg();

        // 清除缓存
        cacheSizeTxt = (TextView) findViewById(R.id.tv_cache_size);
        double size = getDirSize(ImageLoader.getInstance().getDiskCache().getDirectory());
        cacheSizeTxt.setText(new DecimalFormat("0.00").format(size) + "M");

        // 读取当前配置中的亮度信息
        int obv = XichuangzhuApplication.getInstance().getBrightness();
        BrightnessUtil.setBrightness(SettingActivity.this, obv);

        lightSkBar = (SeekBar) findViewById(R.id.light_skbar);
        lightSkBar.setProgress(obv);
        lightSkBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int nbv = seekBar.getProgress();
                XichuangzhuApplication.getInstance().saveBrightness(nbv);
                BrightnessUtil.setBrightness(SettingActivity.this, nbv);
            }
        });
    }

    private double getDirSize(File file) {
        //判断文件是否存在
        if (file.exists()) {
            //如果是目录则递归计算其内容的总大小
            if (file.isDirectory()) {
                File[] children = file.listFiles();
                double size = 0;
                for (File f : children)
                    size += getDirSize(f);
                return size;
            } else {//如果是文件则直接返回其大小,以“兆”为单位
                double size = (double) file.length() / 1024 / 1024;
                return size;
            }
        } else {
            return 0.0;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initLoginCfg();
    }

    private void initLoginCfg() {
        if (AVUser.getCurrentUser() == null) {
            loginSettingLayout.setVisibility(View.GONE);
            logoutLayout.setVisibility(View.GONE);
        } else {
            loginSettingLayout.setVisibility(View.VISIBLE);
            logoutLayout.setVisibility(View.VISIBLE);
        }

        // 读取简繁转换信息
        String ftv = XichuangzhuApplication.getInstance().getFanjian();
        if ("1".equals(ftv)) {
            fontType.setText(getString(R.string.chines));
        } else {
            fontType.setText(getString(R.string.tw));
        }

        // 读取摘录字体信息
        int fsv = XichuangzhuApplication.getInstance().getFontStyle();
        if (fsv == 0) {
            fontStyle.setText(getString(R.string.wenyuegutifangsong));
        } else {
            fontStyle.setText(getString(R.string.huawenfangsong));
        }
    }

    public void clickView(View view) {
        switch (view.getId()) {
            case R.id.my_info_btn:
                // 账号和密码
                startActivity(new Intent(this, ChangePhonePwdActivity.class));
                break;
            case R.id.my_msg_btn:
                // 消息通知
                startActivity(new Intent(this, PushMsgActivity.class));
                break;
            case R.id.my_switch_btn:
                // 简繁转换
                startActivity(new Intent(this, SwitchFontTypeActivity.class));
                break;
            case R.id.my_font_btn:
                // 摘录字体
                startActivity(new Intent(this, SwitchFontStyleActivity.class));
                break;
            case R.id.my_clear_btn:
                // 清除缓存
                final Dialog loadingDialog = DialogFactory.createLoadingDialog(SettingActivity.this, "正在清除缓存");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        AVQuery.clearAllCachedResults();
                        ImageLoader.getInstance().clearMemoryCache();
                        ImageLoader.getInstance().clearDiskCache();
                        cacheSizeTxt.setText("0.0M");
                        showToastInfo(getString(R.string.cache_already_clear));
                        loadingDialog.dismiss();
                    }
                }, 2000);
                break;
            case R.id.my_logout_btn:

                new AlertDialog.Builder(SettingActivity.this)
                        .setMessage(getString(R.string.login_out_sure))
                        .setTitle("提示")
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 退出登录
                                dialog.dismiss();
                                AVUser.logOut();
                                startActivity(new Intent(SettingActivity.this, LoginActivity.class));
                                finish();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).show();

                break;
        }
    }
}
